<template>
	<el-container>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0"
							   @click="batch_del"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-select clearable placeholder="请选择部门"
								   v-model="search.deptId" filterable style="width: 100%">
							<el-option v-for="item in depts" :key="item.id" :label="item.name" :value="item.id"/>
						</el-select>
						<el-input v-model="search.name" placeholder="接口名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :data="tableData" :total="total" @selection-change="selectionChange"
						 @getTableData="getTableData"
						 stripe remoteSort
						 remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="ID" prop="id" width="80" sortable></el-table-column>
					<el-table-column label="接口名称" prop="name" width="200" sortable show-overflow-tooltip
									 column-key="account"></el-table-column>
					<el-table-column label="地址" prop="url" width="300" sortable
									 show-overflow-tooltip></el-table-column>
					<el-table-column label="创建时间" prop="createDatetime" width="150" sortable></el-table-column>
					<el-table-column label="更新时间" prop="updateDatetime" width="150" sortable></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small"
										   @click="table_show(scope.row, scope.$index)">查看
								</el-button>
								<el-button text type="primary" size="small"
										   @click="table_edit(scope.row, scope.$index)">编辑
								</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>

				</scTable>


			</el-main>
		</el-container>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'
	import {deleteApi, getApiById, getApiList} from "@/api/model/jk";
	import {getDeptList} from "@/api/model/dept";

	export default {
		name: 'user',
		components: {
			saveDialog
		},
		data() {
			return {
				dialog: {
					save: false
				},
				showGrouploading: false,
				groupFilterText: '',
				group: [],
				tableData: [],
				pageNum: 1,
				pageSize: 10,
				total: 0,
				selection: [],
				search: {
					name: null,
					deptId: null,
				},
				depts: []
			}
		},
		watch: {
			groupFilterText(val) {
				this.$refs.group.filter(val);
			}
		},
		mounted() {
			this.init();
			this.getGroup()
			this.getDept();
		},
		methods: {
			getTableData(data) {
				this.pageNum = data.pageNum
				this.pageSize = data.pageSize
				this.init()
			},
			init() {
				getApiList({
					"pageNum": this.pageNum,
					"pageSize": this.pageSize,
					"name": this.search.name,
					"deptId": this.search.deptId
				}).then((res) => {
					let list = res.data;
					this.tableData = list
					console.log(res)
				})
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					getApiById({'id': row.id}).then(res => {
						this.$refs.saveDialog.open('edit').setData(res.data)
					})
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					getApiById({'id': row.id}).then(res => {
						this.$refs.saveDialog.open('show').setData(res.data)
					})
				})
			},
			//删除
			async table_del(row) {
				deleteApi({'ids': row.id}).then(res => {
					if (res.code == 200) {
						this.$message.success("删除成功")
						this.init()
					} else {
						this.$alert(res.msg, "提示", {type: 'error'})
					}
				})
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					let ids = []
					this.selection.forEach(item => {
						ids.push(item.id)
					})
					console.log(ids.join(','))
					deleteApi({'ids': ids.join(',')}).then(res => {
						if (res.code === 200) {
							this.$message.success("删除成功")
							this.init()
						} else {
							this.$alert(res.msg, "提示", {type: 'error'})
						}
					})
					loading.close();
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//加载树数据
			async getGroup() {
				this.showGrouploading = true;
				var res = await this.$API.system.dept.list.get();
				this.showGrouploading = false;
				var allNode = {id: '', label: '所有'}
				res.data.unshift(allNode);
				this.group = res.data;
			},
			async getDept() {
				getDeptList({
					"pageNum": 1,
					"pageSize": 99
				}).then(res => {
					this.depts = res.data.list;
				})
			},
			//树过滤
			groupFilterNode(value, data) {
				if (!value) return true;
				return data.label.indexOf(value) !== -1;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					groupId: data.id
				}
				this.$refs.table.reload(params)
			},
			//搜索
			upsearch() {
				this.init();
			},
			//本地更新数据
			handleSuccess() {
				this.init();
				// if (mode == 'add') {
				// 	data.id = new Date().getTime()
				// 	this.$refs.table.tableData.unshift(data)
				// } else if (mode == 'edit') {
				// 	this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
				// 		Object.assign(item, data)
				// 	})
				// }
			}
		}
	}
</script>

<style>
</style>
